﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BMSQ.Utility;

namespace BMSQ.Admin.Accounts
{
    public partial class RoleNodes : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoles();
            }
        }

        private void BindRoles()
        {
            BindDrowDownList(ddlRoles, Vincent.Permission.Functions.Account.GetRoles(""), "rolename", "roleid", true);
        }

        protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindNodes();

            BindRoleCheckedNodes();
        }

        private void BindNodes()
        {
            List<Vincent.Permission.Model.account_nodeslist> lst = Vincent.Permission.Functions.Account.GetMenus("");  //new BLL.account_nodeslist().GetModelList("");
            List<Vincent.Permission.Model.account_nodeslist> lstRoot = lst.Where(p => p.ParentID == "").OrderBy(p => p.OrderID).ToList();

            tv.Nodes.Clear();

            foreach (Vincent.Permission.Model.account_nodeslist node in lstRoot)
            {
                TreeNode tn = new TreeNode();
                tn.Text = node.Text;
                tn.ToolTip = node.NodeID;

                BindRoleNodes(lst, node.NodeID, tn);

                tv.Nodes.Add(tn);
            }

            tv.ExpandAll();
        }

        private void BindRoleNodes(List<Vincent.Permission.Model.account_nodeslist> lstAll, string rootNodeID, TreeNode rootTreeNode)
        {
            List<Vincent.Permission.Model.account_nodeslist> lst = lstAll.Where(p => p.ParentID == rootNodeID).OrderBy(p => p.OrderID).ToList();
            foreach (Vincent.Permission.Model.account_nodeslist node in lst)
            {
                TreeNode tn = new TreeNode();
                tn.Text = node.Text;
                tn.ToolTip = node.NodeID;

                BindRoleNodes(lstAll, node.NodeID, tn);

                rootTreeNode.ChildNodes.Add(tn);
            }
        }

        private void BindRoleCheckedNodes()
        {
            if (ddlRoles.SelectedIndex > 0)
            {
                List<Vincent.Permission.Model.account_rolenodes> lst = Vincent.Permission.Functions.Account.GetRoleMenus(ddlRoles.SelectedValue); //new BLL.account_rolenodes().GetRoleNodes(ddlRoles.SelectedValue);
                foreach (TreeNode tn in tv.Nodes)
                {
                    BindTreeNodes(lst, tn);
                }
            }
        }

        private void BindTreeNodes(List<Vincent.Permission.Model.account_rolenodes> lstAll, TreeNode rootTreeNode)
        {
            if (lstAll.Where(p => p.NodeID == rootTreeNode.ToolTip).ToList().Count > 0)
            {
                rootTreeNode.Checked = true;
            }

            if (rootTreeNode.ChildNodes.Count > 0)
            {
                foreach (TreeNode tn in rootTreeNode.ChildNodes)
                {
                    BindTreeNodes(lstAll, tn);
                }
            }
        }

        private void SaveRoleInNodes()
        {
            List<Vincent.Permission.Model.account_rolenodes> lst = new List<Vincent.Permission.Model.account_rolenodes>();
            foreach (TreeNode tn in tv.Nodes)
            {
                SelectRoleInNodes(lst, tn);
            }

            Vincent.Permission.Functions.Account.DeleteRoleMenus(ddlRoles.SelectedValue);
            Vincent.Permission.Functions.Account.SetRoleMenus(lst);
        }

        private void SelectRoleInNodes(List<Vincent.Permission.Model.account_rolenodes> lstAll, TreeNode rootTreeNode)
        {
            if (rootTreeNode.Checked)
            {
                lstAll.Add(new Vincent.Permission.Model.account_rolenodes() { NodeID = rootTreeNode.ToolTip, RoleID = ddlRoles.SelectedValue });
            }

            if (rootTreeNode.ChildNodes.Count > 0)
            {
                foreach (TreeNode tn in rootTreeNode.ChildNodes)
                {
                    SelectRoleInNodes(lstAll, tn);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlRoles.SelectedIndex > 0)
            {
                SaveRoleInNodes();

                MessageBox.Show(this.Page, "更新成功");
            }
        }
    }
}